//
//  TanChuangView.m
//  TanchuangDemo
//
//  Created by make on 2017/3/7.
//  Copyright © 2017年 maple. All rights reserved.
//

#import "TanChuangView.h"
@interface TanChuangView()
@property (nonatomic,strong)UIView *containerView;
@property (nonatomic,strong)UIView *topView;

@end
@implementation TanChuangView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2f];
        [self configView];

    }
    return self;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        
        [self configView];
        
    }

    return self;
}


- (void)configView{
    
    
    
    __weak typeof(self) weakSelf = self;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(weakSelf.mas_left).mas_offset(30);
        make.right.equalTo(weakSelf.mas_right).mas_offset(-30);
        make.centerY.equalTo(weakSelf.mas_centerY).mas_offset(-20);
        make.height.mas_equalTo(200);
    }];
    
    
  
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView);
        make.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(50);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(weakSelf.topView);
        make.top.equalTo(weakSelf.topView);
        make.right.equalTo(weakSelf.topView);
        make.height.equalTo(weakSelf.topView);
    }];
    
    [self.pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.containerView.mas_left).mas_offset(20);
        make.right.equalTo(weakSelf.containerView.mas_right).mas_offset(-20);
        make.top.equalTo(weakSelf.topView.mas_bottom).mas_offset(25);
        make.height.mas_equalTo(50);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(weakSelf.containerView.mas_right);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.mas_equalTo(50);
        make.width.equalTo(weakSelf.cancelBtn);
        
    }];
    
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.sureBtn.mas_left);
        make.left.equalTo(weakSelf.containerView.mas_left);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.equalTo(weakSelf.sureBtn);
    }];

}

#pragma makr - 点击事件
- (void)sureClick:(UIButton *)sender{
    
    if ([_pwdView getPwdString].length == 4) {
        [self endEditing:YES];
        NSLog(@"获取到的密码是：%@",[_pwdView getPwdString]);
        if (self.pressSureBtn) {
            
            self.pressSureBtn([_pwdView getPwdString]);
        }
    }else{
        
        NSLog(@"密码不足4位数");
    
    }
}


- (void)cancelClick:(UIButton*)sender{
    
    NSLog(@"点击了取消");
    [self endEditing:YES];
    if (self.pressCancelBtn) {
        self.pressCancelBtn();
    }
}

#pragma mark - 动画
- (void)outWithAnimation{
    
    typeof (self)weakSelf = self;
        [UIView animateWithDuration:.35f animations:^{
            CGRect frame =  weakSelf.frame;
            frame.origin.y = weakSelf.frame.size.height;
            weakSelf.frame = frame;
            
        } completion:^(BOOL finished) {
            if (finished) {
                [weakSelf removeFromSuperview];
            }
        }];

}

#pragma mark - 懒加载
- (UIView *)containerView{

    if (!_containerView) {
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.containerView];
        self.containerView.layer.masksToBounds = YES;
        self.containerView.layer.cornerRadius = 10.0f;
    }
    return _containerView;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f];
        [self.containerView addSubview:self.topView];
    }
    return _topView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:17];
        [self.topView addSubview:self.titleLabel];
    }
    return _titleLabel;
}

- (FIDPwdCollectionView *)pwdView{
    if (!_pwdView) {
        _pwdView = [[FIDPwdCollectionView alloc]init];
        [self.containerView addSubview:_pwdView];
        _pwdView.numberOfPwdView = 4;
        _pwdView.minimumLineSpacing = 10;
        _pwdView.textColor = [UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f];
        _pwdView.normalBorderColor = [UIColor groupTableViewBackgroundColor];
        _pwdView.selectBorderColor = [UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f];
        _pwdView.borderWidth = 2.f;
    }
    return _pwdView;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f] forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.layer.borderWidth = .3f;
        _sureBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [self.containerView addSubview:_sureBtn];
        
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _cancelBtn.layer.borderWidth = .3f;
        _cancelBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}
@end
